package com.agu.mgt.code.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.entity.code.CodConfigEntity;
import com.agu.entity.code.CodTableEntity;
import com.agu.mgt.code.service.CodConfigService;
import com.agu.mgt.code.service.CodTableService;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.mgt.sys.dto.CodeVo;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.Query;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 代码生成表
 * 
 * @author
 * @email
 * @date 2019-01-11 16:14:56
 */
@SuppressWarnings("rawtypes")
@RestController
@Api("代码生成表")
@RequestMapping("codtable")
public class CodTableController extends AbstractController {
	@Autowired
	private CodTableService codTableService;
	@Autowired
	private CodConfigService codConfigService;
	

	
	/**
	 * 列表
	 */
	@RequestMapping("/configList")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("codtable:list")
	public Result configList(){

		List<CodConfigEntity> list = codConfigService.queryList(null);
		
		return ResultGenerator.successData(list);
	}
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("codtable:list")
	public Result list(@RequestBody Map params) {

		// 查询列表数据
		Query query = new Query(params);
		Page<CodTableEntity> page = codTableService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}


	@SysLog("代码生成")
	@RequestMapping("/toCode")
	@RequiresPermissions("codtable:list")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String[] tableNames = new String[]{};
		//获取表名，不进行xss过滤
		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String config = orgRequest.getParameter("config");
		
		CodeVo cc = JSON.parseObject(config,CodeVo.class);
		
		byte[] data = codTableService.generatorCode(cc);
		
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");  
        response.addHeader("Content-Length", "" + data.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());  
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改代码生成表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("codtable:update")
	public Result update(@RequestBody CodTableEntity codTable) {

		codTableService.updateTableComments(codTable);

		// 处理日志
		Map<String, String> cmap = new HashMap<String, String>();

		cmap.put("tableName", "表名");
		cmap.put("tableComment", "备注");

		String remark = CompareUtils.getObjectRemark(codTable, cmap);
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
}
